package cn.minhx.infrastructure.adapter.repository;

import cn.minhx.domain.activity.adapter.IActivityRepository;
import cn.minhx.domain.activity.model.valobj.DiscountTypeEnum;
import cn.minhx.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.minhx.domain.activity.model.valobj.SCSkuActivityVO;
import cn.minhx.domain.activity.model.valobj.SkuVO;
import cn.minhx.infrastructure.dao.IGroupBuyActivityDAO;
import cn.minhx.infrastructure.dao.IGroupBuyDiscountDAO;
import cn.minhx.infrastructure.dao.ISCSkuActivityDAO;
import cn.minhx.infrastructure.dao.ISkuDAO;
import cn.minhx.infrastructure.dao.po.GroupBuyActivity;
import cn.minhx.infrastructure.dao.po.GroupBuyDiscount;
import cn.minhx.infrastructure.dao.po.SCSkuActivity;
import cn.minhx.infrastructure.dao.po.Sku;
import cn.minhx.infrastructure.dcc.DCCService;
import cn.minhx.infrastructure.redis.IRedisService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBitSet;
import org.springframework.stereotype.Repository;

/**
 * 活动仓储
 *
 * @author min
 */
@Slf4j
@Repository
public class ActivityRepository implements IActivityRepository {

	@Resource
	private ISCSkuActivityDAO skuActivityDAO;
	@Resource
	private IGroupBuyActivityDAO groupBuyActivityDAO;
	@Resource
	private IGroupBuyDiscountDAO groupBuyDiscountDAO;
	@Resource
	private ISkuDAO skuDAO;
	@Resource
	private IRedisService redisService;
	@Resource
	private DCCService dccService;

	@Override
	public SCSkuActivityVO querySCSkuActivityBySCGoodsId(String source, String channel, String goodsId) {
		SCSkuActivity scSkuActivityReq = new SCSkuActivity();
		scSkuActivityReq.setSource(source);
		scSkuActivityReq.setChannel(channel);
		scSkuActivityReq.setGoodsId(goodsId);
		SCSkuActivity scSkuActivity = skuActivityDAO.querySCSkuActivityBySCGoodsId(scSkuActivityReq);
		if (null == scSkuActivity)
			return null;
		return SCSkuActivityVO.builder().source(scSkuActivity.getSource()).chanel(scSkuActivity.getChannel())
				.activityId(scSkuActivity.getActivityId()).goodsId(scSkuActivity.getGoodsId()).build();
	}

	@Override
	public GroupBuyActivityDiscountVO queryGroupBuyActivityDiscountVO(Long activityId) {
		// 根据SC渠道值查询配置中最新的1个有效的活动
		GroupBuyActivity groupBuyActivity = groupBuyActivityDAO.queryValidGroupBuyActivityId(activityId);
		if (null == groupBuyActivity) {
			log.info("未查询到生效的活动 activityId:{}", activityId);
			return null;
		}
		String discountId = groupBuyActivity.getDiscountId();
		GroupBuyDiscount groupBuyDiscountPO = groupBuyDiscountDAO.queryGroupBuyActivityDiscountByDiscountId(discountId);
		GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount = GroupBuyActivityDiscountVO.GroupBuyDiscount
				.builder().discountName(groupBuyDiscountPO.getDiscountName())
				.discountDesc(groupBuyDiscountPO.getDiscountDesc())
				.discountType(DiscountTypeEnum.get(groupBuyDiscountPO.getDiscountType()))
				.marketPlan(groupBuyDiscountPO.getMarketPlan()).marketExpr(groupBuyDiscountPO.getMarketExpr())
				.tagId(groupBuyDiscountPO.getTagId()).build();
		return GroupBuyActivityDiscountVO.builder().activityId(groupBuyActivity.getActivityId())
				.activityName(groupBuyActivity.getActivityName()).groupBuyDiscount(groupBuyDiscount)
				.groupType(groupBuyActivity.getGroupType()).takeLimitCount(groupBuyActivity.getTakeLimitCount())
				.target(groupBuyActivity.getTarget()).validTime(groupBuyActivity.getValidTime())
				.status(groupBuyActivity.getStatus()).startTime(groupBuyActivity.getStartTime())
				.endTime(groupBuyActivity.getEndTime()).tagId(groupBuyActivity.getTagId())
				.tagScope(groupBuyActivity.getTagScope()).build();
	}

	@Override
	public SkuVO querySkuByGoodsId(String goodsId) {
		Sku sku = skuDAO.querySkuByGoodsId(goodsId);
		return SkuVO.builder().goodsId(sku.getGoodsId()).goodsName(sku.getGoodsName())
				.originalPrice(sku.getOriginalPrice()).build();
	}

	@Override
	public boolean withinCrowdTag(String tagId, String userId) {
		RBitSet bitSet = redisService.getBitSet(tagId);
		if (!bitSet.isExists()) {
			return false;
		}
		// 判断用户是否存在人群中
		return bitSet.get(redisService.getIndexFromUserId(userId));
	}

	@Override
	public boolean downgradeSwitch() {
		return dccService.isDowngradeSwitch();
	}

	@Override
	public boolean cutRange(String userId) {
		return dccService.isCutRange(userId);
	}
}
